package org.fuys.coder.domain.auth.service.handler.register.impl;

import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.infrastructure.model.dto.UserDTO;
import org.fuys.coder.domain.auth.repository.ICommonRepository;
import org.fuys.coder.domain.auth.service.handler.register.RegisterHandler;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.auth.service.handler.register.impl
 * @className: EmailCheckHandler
 * @author: WangJing
 * @description:
 * @date: 2024/6/19 18:52
 * @version: 1.0
 */
@Service
public class SaveUserHandler implements RegisterHandler {

    @Resource
    private ICommonRepository commonRepository;

    @Override
    public boolean handle(UserDTO userDTO) throws BusinessException {
        final int i;
        try {
            i = commonRepository.insertUser(userDTO.getEmail(),
                    userDTO.getPassword(), userDTO.getUsername(),userDTO.getAvatar(), userDTO.getPoint());
        } catch (DuplicateKeyException e) {
            throw new BusinessException("邮箱已经被注册过!");
        }
        userDTO.setId(i);
        return true;
    }
}
